package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.result.Result;
import com.dongdongshop.result.ResultEnum;
import com.dongdongshop.service.ContentCategoryService;
import com.dongdongshop.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("category")
public class CategoryController {

    private static final Logger logger = LoggerFactory.getLogger(CategoryController.class);

    @Reference
    private ContentCategoryService contentCategoryService;

    @RequestMapping("toCategoryShow")
    public String toCategoryShow(){
        return "admin/content_category";
    }

    @RequestMapping("getListContentCategory")
    @ResponseBody
    public Result<PageUtil<TbContentCategory>> getListContentCategory(@RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
                                                                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
        try {
            PageInfo<TbContentCategory> pageInfo = contentCategoryService.getListContentCategory(pageNumber,pageSize);
            PageUtil<TbContentCategory> pageUtil = new PageUtil<>();
            pageUtil.setPageNumber(pageNumber);
            pageUtil.setPageSize(pageSize);
            pageUtil.setList(pageInfo.getList());
            pageUtil.setTotal(pageInfo.getTotal());
            return Result.jsonResponse(ResultEnum.OK).setData(pageUtil);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("insertCategory")
    @ResponseBody
    private Result insertCategory(TbContentCategory tbContentCategory){
        try {
            int i = contentCategoryService.insertCategory(tbContentCategory);
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    //修改时数据回显
    @RequestMapping("toUpdateCategory")
    @ResponseBody
    public Result toUpdateBrand(@RequestParam("id") Long id){
        try {
            TbContentCategory contentCategory = contentCategoryService.selectCateById(id);
            return Result.jsonResponse(ResultEnum.OK).setData(contentCategory);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("updateCategory")
    @ResponseBody
    public Result updateCategory(TbContentCategory contentCategory){
        try {
            int i = contentCategoryService.updateCategory(contentCategory);
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    //通过id批量删除数据
    @RequestMapping("deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestParam("ids[]") Long[] ids){
        try {
            int i = contentCategoryService.deleteBatch(ids);
            return Result.jsonResponse(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

}
